package com.zp.crm.service.MarketingService.Impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.zp.crm.dao.MarketingDao.MarketingDao;
import com.zp.crm.dao.UserDao.UserDao;
import com.zp.crm.dto.Marketing.MarketingDto;
import com.zp.crm.entity.Marketing.MarketingEntity;
import com.zp.crm.entity.User.UserEntity;
import com.zp.crm.service.MarketingService.MarketingService;
import com.zp.crm.service.MenuService.MenuService;
import com.zp.crm.util.targetLog.Log;
@Service("marService")
public class MarketingServiceImple implements MarketingService {
	
	@Resource
	public MarketingDao marDao;
	@Resource
	public MenuService mservice;
	@Resource
	public UserDao userDao;

	@Log(name="销售机会分页查询，封装html")
	@Override
	public Map<String, Object> selectPageSizeList(int page, int size) {
		
		Map<String, Object> map=new HashMap<String,Object>();
		
		int selectcount=marDao.selectByCount();
		int PageIndex=(page-1)*10;
		List<MarketingEntity> list=marDao.selectByList(PageIndex,size);
		
		if(list.size()>0){
			MarketingDto dto=new MarketingDto();
			
			String obj=dto.Text(selectcount,list,page,0);
			
			map.put("success", true);
			
			map.put("message", obj);
			return map;
		}else{
			map.put("success", false);
			
			map.put("message", "暂无此信息！");
			return map;
		}
	}
	@Log(name="删除指定的销售信息 ")
	@Override
	public Map<String, Object> deleteByKeyCode(String code) {
		Map<String, Object> map=new HashMap<String, Object>();
		
		int deleter=marDao.deleteByKeyCode(code);
		
		if(deleter>0){
			map.put("success", true);
			map.put("message", "删除成功！");
		}else{
			map.put("success", false);
			map.put("message", "删除失败！请刷新后从试！");
		}
		return map;
	}
	@Log(name="按条件查询销售机会信息")
	@Override
	public Map<String, Object> SelectpageCreate(String name, String mar, String phoneName, int page, int size) {
		Map<String, Object> map=new HashMap<>();
		
		int PageIndex=(page-1)*size;
		
		if(name != null || mar != null || phoneName != null){
			
			if(name.length()>0 || mar.length()>0 || phoneName.length()>0){
				int selectcount=marDao.selectCreateCount(name,mar,phoneName);
				
				List<MarketingEntity> list=marDao.selectCreateByList(name,mar,phoneName,PageIndex,size);
				
				String obj=new MarketingDto().Text(selectcount, list, page,1);
				
				map.put("success", true);
				
				map.put("message", obj);
			}else{
				map=selectPageSizeList(page,size);
			}
		}else{
			map=selectPageSizeList(page,size);
		}
		
		return map;
	}
	@Log(name="销售进行指定")
	@Override
	public Map<String, Object> selectEditCreateBycode(String code,String user_id) {
		 SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 Map<String, Object> map=new HashMap<>();
		 
		 MarketingEntity entity=marDao.selectEditCreateBycode(code);
		 
		 if(entity != null){
			 boolean t=false;
			 t=mservice.selectIndexofRoid(user_id,"营销角色");
			if(t){
				entity.setStrTime(f.format(entity.getMarCreateTime()));
				map.put("success", true);
				map.put("message", entity);
				map.put("mesccuess", true);
			}else{
				entity.setStrTime(f.format(entity.getMarCreateTime()));
				map.put("success", true);
				map.put("message", entity);	 
			}
		 }else{
			map.put("success", false);
			map.put("message", "不存在此信息！请刷新后从试!");	 
		 }
		return map;
	}
	@Log(name="添加销售机会信息 ")
	@Override
	public Map<String, Object> MarInsert(MarketingEntity entity) {
		Map<String, Object> map=new HashMap<>();
		boolean c=true;
		if(entity.getCode()==null || entity.getCode().equals("")){
			c=false;
		}
		if(entity.getSourceCode()==null || entity.getSourceCode().equals("")){
			c=false;
		}
		if(entity.getMarName()==null || entity.getMarName().equals("")){
			c=false;
		}
		if(entity.getMarPhone()==null || entity.getMarPhoneName().equals("")){
			c=false;
		}
		if(entity.getMarPhoneName()==null || entity.getMarPhoneName().equals("")){
			c=false;
		}
		if(c){
			entity.setMarzpstuts("未指派");
			int r=marDao.inset(entity);
			if(r>0){
				map.put("success", true);
				map.put("message", "添加信息成功！");
			}else{
				map.put("success", false);
				map.put("message", "添加信息失败！请从试！");
			}
			
		}else{
			map.put("success", false);
			map.put("message", "请正确提交信息！");
		}
		
		return map;
	}
	@Log(name="修改销售机会信息 ")
	@Override
	public Map<String, Object> MarEdit(MarketingEntity entity) {
		Map<String, Object> map=new HashMap<>();
		boolean c=true;
		if(entity.getCode()==null || entity.getCode().equals("")){
			c=false;
		}
		if(entity.getSourceCode()==null || entity.getSourceCode().equals("")){
			c=false;
		}
		if(entity.getMarName()==null || entity.getMarName().equals("")){
			c=false;
		}
		if(entity.getMarPhone()==null || entity.getMarPhoneName().equals("")){
			c=false;
		}
		if(entity.getMarPhoneName()==null || entity.getMarPhoneName().equals("")){
			c=false;
		}
		if(c){
			int r=marDao.MarEdit(entity);
			if(r>0){
				map.put("success", true);
				map.put("message", "修改信息成功！");
			}else{
				map.put("success", false);
				map.put("message", "修改信息失败！请从试！");
			}
			
		}else{
			map.put("success", false);
			map.put("message", "请正确提交信息！");
		}
		return map;
	}
	@Log(name="进行销售机会指定 ")
	@Override
	public Map<String, Object> MarZdEdit(String code, String name, String stuts) {
		Map<String, Object> map=new HashMap<>();
		int r=marDao.MarZdEdit(code,name,stuts,new Date());
		if(r>0){
			map.put("success", true);
			map.put("message", "指定信息成功！");
		}else{
			map.put("success", false);
			map.put("message", "指定信息失败！");
		}
		return map;
	}
	@Log(name="查询当前用户登录所用有的销售机会")
	@Override
	public Map<String, Object> LoadUserMarList(String user_id,int page,int size) {
		Map<String, Object> map= new HashMap<String, Object>();
		UserEntity user=userDao.selectBykeyCode(user_id);
		if(user != null && user.getUserTrueName() != null){
			int PageIndex=(page-1)*size;
			int selectcount=marDao.selectByKeyUserTrueNameCount(user.getUserTrueName());
			List<MarketingEntity> list=marDao.selectByKeyUserTrueName(user.getUserTrueName(),PageIndex,size);
			String obj=new MarketingDto().DevelopText(selectcount,list,page,0);
			map.put("success", true);
			map.put("message", obj);
		}else{
			map.put("success", false);
		}
		return map;
	}
	@Log(name="查询销售机会开发信息 ")
	@Override
	public Map<String, Object> LoadUserSelectCreateMarList(String user_id, String name, String mar, String phoneName,
			int page, int size) {
		Map<String, Object> map= new HashMap<String, Object>();
		UserEntity user=userDao.selectBykeyCode(user_id);
		if(user != null && user.getUserTrueName() != null){
			int PageIndex=(page-1)*size;
			int selectcount=marDao.LoadUserSelectCreateMarListCount(user.getUserTrueName(),name,mar,phoneName);
			List<MarketingEntity> list=marDao.LoadUserSelectCreateMarList(user.getUserTrueName(),PageIndex,size,name,mar,phoneName);
			String obj=new MarketingDto().DevelopText(selectcount,list,page,1);
			map.put("success", true);
			map.put("message", obj);
		}else{
			map.put("success", false);
		}
		return map;
	}
	@Log(name="修改销售机会状态 ")
	@Override
	public Map<String, Object> DevelopUpStuts(String code, String stuts) {
		Map<String, Object> map=new HashMap<String,Object>();
		if(code !=null && !code.equals("")){
			int r=marDao.DevelopUpStuts(code,stuts);
			if(r>0){
				map.put("success", true);
				map.put("message", "此信息开发正式启动！");
			}else{
				map.put("success", false);
				map.put("message", "系统繁忙！请从试！");
			}
		}else{
			map.put("success", false);
			map.put("message", "执行失败请从试！");
		}
		return map;
	}
}
